<?php
if (!defined('PRODUCTION')) die ("This resource is unavailable and/or is a server end-point");
/**
 * This file contains all classes required for URI processing
 * No page can be output without being passed through this file
 *
 *     @author Agrim Gupta
 *      @since 0.0.1
 *    @package OrangeRinds
 * @subpackage Classes
 *   @category URI Processing
 */

/**
 * Only an object needs to be created with the requested uri as the supplied argument
 * and the rest is taken care by the constructor
 *
 * @author Agrim Gupta
 */
class URISplit{
    /** @var string Holds the Requested URI ($_SERVER['REQUEST_URI']) */
    var $REQ_URI;

    /** @var string Holds the entire path requested (before the '?' mark) */
    var $REQ_URI_PATH;

    /** @var array Holds the path elements of a URI as a array */
    var $REQ_URI_PATH_E;

    /** @var int Length of the $REQ_URI_PATH_E array */
    var $REQ_URI_PATH_E_LEN;

    /** @var string Holds the name of the webpage that has been entered in the URI (optional in case of index.php) */
    var $REQ_URI_PAGE_NAME;

    /** @var int Stores the length of $REQ_URI_PAGE_PARAM, therefore tell us the number of parameters of the current page */
    var $REQ_URI_PAGE_PARAM_LEN;

    /** @var array Stores the keys of the page parameters supplied in the $_REQUEST variable */
    var $REQ_URI_PAGE_PARAM_KEYS;

    /** @var string Stores the corresponding values of the keys stored in $REQ_URI_PAGE_PARAM_KEYS */
    var $REQ_URI_PAGE_PARAM_VALUES;

    /**
     * Constructor for the URISplit class. Does everything that is needed to do.
     * @param string $uri Contains a trimmed Request URI
     */
    function __construct($uri) {
        parent::__construct();
        $this->REQ_URI = $uri;
        $this->process_uri();
    }

    /** Basic processing functions. No need to explain, understand or change anything here. */
    function process_uri() {
        $this->split_uri();
        $this->split_path();
        $this->split_page();
    }

    /**
     * Splits the URI
     */
    function split_uri() {
        $uri_split_qmark = explode('?', $this->REQ_URI);
        $this->REQ_URI_PATH = $uri_split_qmark[0];
    }

    /**
     * Splits the Path requested in the path URI
     */
    function split_path() {
        $uri_path_split = explode('/', $this->REQ_URI_PATH);

        $i = 0;
        $uri_path_split_len = count($uri_path_split);

        if($uri_path_split[0] == '') $i = 1;
        if($uri_path_split[$uri_path_split_len - 1] == '') $uri_path_split_len--;

        $this->REQ_URI_PATH_E_LEN = $uri_path_split_len - $i;

        for( $j = 0; $j < $this->REQ_URI_PATH_E_LEN; $i++, $j++) {
            $this->REQ_URI_PATH_E[$j] = $uri_path_split[$i];
        }
    }

    function split_page() {
        $this->REQ_URI_PAGE_PARAM_KEYS   = array_keys($_REQUEST);
        $this->REQ_URI_PAGE_PARAM_VALUES = array_values($_REQUEST);
        $this->REQ_URI_PAGE_PARAM_LEN    = count($this->REQ_URI_PAGE_PARAM_KEYS);

        //code for finding page filename
        /*$page_regex = "[^/?*:;{}\\]+\\.[^/?*:;{}\\]";
        echo $page_regex;
        $len  = $this->REQ_URI_PATH_E_LEN;
        $page = $this->REQ_URI_PATH_E[$len - 1];
        echo $page;
        if ( preg_match ( $page_regex , $page ) ) {
            echo "page requested";
        }
        else {
            echo "Folder requested";
        }*/
    }

    function display_uri_path_elements() {
        $uri_path_e_length = count($this->REQ_URI_PATH_E);
        for( $i = 0; $i < $uri_path_e_length; $i++) {
            echo "<P> --> $i . " . $this->REQ_URI_PATH_E[$i] . "</P>";
        }
    }
}
?>